<template>
  <div class="common_wrap">
    <div class="common_title">
      自主申报工作详情
      <div class="back_btn" @click="goBack"><i></i><span>返回</span></div>
    </div>
    <div class="qutoa_header">
      <div class="title">{{ quotaInfo.title }}</div>
      <div class="status" :class="formateStateColor(quotaInfo.reviewStatus)">
        {{ formateState(quotaInfo.reviewStatus) }}
      </div>
      <div class="date">{{ formateDate(quotaInfo.gmtCreate) }}</div>
    </div>
    <div class="quota_person">
      <div class="tit">提交人：</div>
      <div>
        {{ quotaInfo.employeeName }}({{ quotaInfo.areaName }}-{{
          quotaInfo.positionName
        }})
      </div>
    </div>
    <div class="quota_content">
      <div class="tit">工作内容：</div>
      <div class="content">{{ quotaInfo.content }}</div>
    </div>
    <div class="quota_file">
      <div class="tit">附件：</div>
      <div class="file_list" v-if="fileItem.annexName">
        <div class="file_item" @click="downloadFile(fileItem)">
          <i class="icon" :class="formateFileType(fileItem.annexName)"></i>
          <div>{{ fileItem.annexName }}</div>
        </div>
      </div>
      <div class="no_file" v-else>暂无附件</div>
    </div>
    <div class="quota_content" v-if="quotaInfo.reviewStatus == '2'">
      <div class="tit">退回原因</div>
      <div class="content">{{ quotaInfo.commont }}</div>
    </div>
    <div class="quota_content" v-if="quotaInfo.reviewStatus == '1'">
      <div class="tit">通过原因</div>
      <div class="content">{{ quotaInfo.commont }}</div>
    </div>
    <div class="btn_box" v-if="quotaInfo.reviewStatus == '0'">
      <el-button type="danger" @click="showDialog">退回</el-button>
      <el-button type="primary" @click="visible2 = true">通过</el-button>
    </div>
    <el-dialog :visible.sync="visible" title="申报退回" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item prop="reason" label="退回原因">
          <el-input
            v-model="form.reason"
            type="textarea"
            maxlength="300"
            :rows="4"
            placeholder="请输入退回原因"
            show-word-limit
            resize="none"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="visible2" title="申报通过" width="600px">
      <el-form ref="form" :model="form2" :rules="rules2" label-width="120px">
        <el-form-item prop="reason" label="通过原因">
          <el-input
            v-model="form2.reason"
            type="textarea"
            maxlength="300"
            :rows="4"
            placeholder="请输入通过原因"
            show-word-limit
            resize="none"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible2 = false">取 消</el-button>
        <el-button type="primary" @click="agree">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ContentManageDetail",
  data() {
    return {
      visible: false,
      form: {
        reason: "",
      },
      rules: {
        reason: [{ required: true, message: "退回原因必填" }],
      },
      visible2: false,
      form2: {
        reason: "",
      },
      rules2: {
        reason: [{ required: true, message: "退回原因必填" }],
      },
      quotaInfo: {},
      fileItem: {},
    };
  },
  computed: {
    formateState() {
      return function (state) {
        switch (state) {
          case "0":
            return "待审核";
          case "1":
            return "已审核";
          default:
            return "未通过";
        }
      };
    },
    formateStateColor() {
      return function (state) {
        switch (state) {
          case "1":
            return "success";
          case "2":
            return "fail";
          default:
            return "";
        }
      };
    },
    formateDate() {
      return function (date) {
        const time = new Date(date);
        const year = time.getFullYear();
        const month =
          time.getMonth() + 1 > 9
            ? time.getMonth() + 1
            : "0" + (time.getMonth() + 1);
        const day = time.getDate() > 9 ? time.getDate() : "0" + time.getDate();
        const hour =
          time.getHours() > 9 ? time.getHours() : "0" + time.getHours();
        const minutes =
          time.getMinutes() > 9 ? time.getMinutes() : "0" + time.getMinutes();
        return year + "-" + month + "-" + day + "  " + hour + ":" + minutes;
      };
    },
    formateFileType() {
      return function (fileName) {
        if (fileName) {
          const arr = fileName.split(".");
          const type = arr[arr.length - 1];
          if (type === "docx" || type === "doc") {
            return "word";
          } else if (type === "xlsx" || type === "xls") {
            return "word";
          } else if (type === "pdf") {
            return "pdf";
          } else {
            return "";
          }
        } else {
          return "";
        }
      };
    },
  },
  created() {
    this.quotaInfo = this.$route.query;
    this.fileItem = {
      annexName: this.quotaInfo.enclosureName,
      filePath: this.quotaInfo.enclosureAddress,
    };
    console.log(this.quotaInfo);
  },
  methods: {
    goBack() {
      history.go(-1);
    },
    agree() {
      const params = {
        _gp: "declaration",
        _mt: "auditPass",
        id: this.quotaInfo.id,
        comment: this.form2.reason,
      };
      this.$api.commonApi(params).then((res) => {
        if (res.data.errno === 200) {
          this.$message.success("提交成功");
          this.visible2 = false;
          setTimeout(() => {
            this.$router.go(-1);
          }, 1500);
        } else {
          this.$message.error("提交失败");
        }
      });
    },
    showDialog() {
      this.visible = true;
    },
    close() {
      this.visible = false;
    },
    confirm() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        const params = {
          _gp: "declaration",
          _mt: "auditFailure",
          id: this.quotaInfo.id,
          comment: this.form.reason,
        };
        this.$api.commonApi(params).then((res) => {
          if (res.data.errno === 200) {
            this.$message.success("提交成功");
            this.visible = false;
            setTimeout(() => {
              this.$router.go(-1);
            }, 1500);
          } else {
            this.$message.fail(res.data.errmsg);
          }
        });
      });
    },
    downloadFile(item) {
      window.open(item.filePath, "_blank");
    },
  },
};
</script>

<style scoped>
.qutoa_header {
  display: flex;
  margin-bottom: 20px;
  color: #333;
}
.qutoa_header .title {
  font-size: 18px;
  font-weight: bold;
  margin-right: 30px;
}
.qutoa_header .status {
  width: 70px;
  height: 22px;
  line-height: 22px;
  text-align: center;
  border: 1px solid #7f7f7f;
  color: #7f7f7f;
  border-radius: 5px;
}
.qutoa_header .status.success {
  border: 1px solid #4ca5f1;
  color: #4ca5f1;
}
.qutoa_header .status.fail {
  border: 1px solid #f60e36;
  color: #f60e36;
}
.qutoa_header .date {
  height: 24px;
  line-height: 24px;
  font-size: 14px;
  color: #999;
  margin-left: 20px;
}
.quota_person {
  display: flex;
  font-size: 14px;
  color: #333;
  margin-bottom: 20px;
}
.quota_person .tit {
  width: 100px;
  text-align: right;
}
.quota_content {
  display: flex;
  margin-bottom: 20px;
}
.quota_content .tit {
  width: 100px;
  text-align: right;
  font-size: 14px;
  color: #333;
  margin-right: 10px;
}
.quota_content .content {
  width: 500px;
  min-height: 100px;
  padding: 10px;
  background-color: #efefef;
  border: 1px solid #dcdcdc;
  border-radius: 5px;
  color: #666;
}
.quota_file {
  display: flex;
  margin-bottom: 20px;
}
.quota_file .tit {
  width: 100px;
  text-align: right;
  font-size: 14px;
  color: #333;
  margin-right: 10px;
}
.quota_file .file_list .file_item {
  display: flex;
  margin-bottom: 10px;
  cursor: pointer;
}
.quota_file .file_list .file_item * {
  cursor: pointer;
}
.quota_file .file_list .file_item .icon {
  display: block;
  width: 26px;
  height: 26px;
  background: url("../../assets/file.png") no-repeat;
  background-size: 100% 100%;
  margin-right: 10px;
}
.quota_file .file_list .file_item .icon.pdf {
  background: url("../../assets/Pdf.png") no-repeat;
  background-size: 100% 100%;
}
.quota_file .file_list .file_item .icon.word {
  background: url("../../assets/word.png") no-repeat;
  background-size: 100% 100%;
}
.quota_file .file_list .file_item .icon.excel {
  background: url("../../assets/excel.png") no-repeat;
  background-size: 100% 100%;
}
.quota_file .file_list .file_item > div {
  width: 400px;
  height: 26px;
  line-height: 26px;
  font-size: 16px;
  color: #666;
}
.quota_file .no_file {
  font-size: 14px;
  color: #999;
}
.btn_box {
  width: 730px;
  display: flex;
  justify-content: center;
}
</style>